import requests


class Actor:
    def __init__(self):
        self.session = requests.Session()
        url = 'http://api.51tickets.com/login'
        body = {"username":"s001","password":"123456","code":"1357","uuid":"5dbe8bad7a1b46e3bd15a7d2082d2f9a"}
        resp = self.session.post(url=url,json=body)
        token = resp.json()['token']
        headers = {'authorization':'Bearer '+ token}
        self.session.headers.update(headers)

    def search_actor_by_id(self,id):
        url = 'http://api.51tickets.com/system/actor/' + str(id)
        result = self.session.get(url=url)
        return result.json()

    def search_actor_by_name(self,actorName):
        url = 'http://api.51tickets.com/system/actor/list'
        params = {
            'pageNum' : 1,
            'pageSize' : 10,
            'actorName'  : actorName
        }
        resp = self.session.get(url=url,params=params)
        return resp.json()

    def add_actor_by_actorName_and_actorDesc(self,actorName,actorDesc):
        url = 'http://api.51tickets.com/system/actor'
        data = {"actorId":None,"actorName":actorName,"actorImg":None,"actorDesc":actorDesc,"followers":None,"boxOffice":None}
        resp = self.session.post(url = url,json=data)
        return resp.json()
    def delete_actor_by_id(self,id):
        url = 'http://api.51tickets.com/system/actor/' + str(id)
        resp = self.session.delete(url)
        return resp.json()

    def update_actor_by_actorName_actorDesc_through_actorId(self,actorId,actorName,actorDesc):
        url = "http://api.51tickets.com/system/actor"
        data = {"searchValue":None,"createBy":None,"createTime":None,"updateBy":None,"updateTime":None,"remark":None,"params":{},"actorId":actorId,"actorName":actorName,"actorImg":None,"actorDesc":actorDesc,"followers":None,"boxOffice":None}
        resp = self.session.put(url = url,json=data)
        return resp.json()


    def upload_imgs_by_path(self,path):
        url = "http://api.51tickets.com/common/upload"
        fp = open(path,mode='rb') #"../imgs/a.jpeg"
        files = {'file':fp}
        resp = self.session.post(url=url,files=files)
        return resp.json()
if __name__ == '__main__':
    actor = Actor()
    # print(actor.search_actor_by_id(126))
    # print(actor.search_actor_by_name('新海诚'))
    # print(actor.add_actor_by_actorName_and_actorDesc('WorldWind_second','The second time to add actor'))
    # print(actor.delete_actor_by_id(263))

    # print(actor.update_actor_by_actorName_actorDesc_through_actorId(126,'worldniwd_actor01','worldwindAddIt'))
    print(actor.upload_imgs_by_path("../imgs/a.jpeg"))